﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DTO;
using System.Data.SqlClient;
using System.Data;

namespace DAO
{
    public class ThamSoDAO
    {
        // them thoi gian cho dat ban
        public int themthoigianchodatban(int thoigiancho)
        {
            int kqload = TonTaiThoiGianChoDatBan();
            if (kqload > 0)
            {
                XoaThoiGianChoDatBan();
            }
            SqlConnection conn = DataProvider.sqlconn;
            conn.Open();
            string sql = "INSERT INTO ThamSo(ThoiGianChoDatBan) VALUES(" + thoigiancho + ")";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.CommandText = sql;
            int kq = cmd.ExecuteNonQuery();
            conn.Close();
            if (kq >= 0)
                return 1;
            else
                return 0;
        }
        //xoa thoi gian cho dat ban
        public void XoaThoiGianChoDatBan()
        {
            SqlConnection conn = DataProvider.sqlconn;
            conn.Open();
            string sql = "Delete ThamSo" ;
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.CommandText = sql;
            int kq = cmd.ExecuteNonQuery();
            conn.Close();
        }
        //ton tai thoi gian cho dat ban
        public int TonTaiThoiGianChoDatBan()
        {
            SqlConnection conn = DataProvider.sqlconn;
            conn.Open();
            string sql = "select COUNT(ThoiGianChoDatBan) from ThamSo";
            SqlCommand cmd = new SqlCommand(sql, conn);
            cmd.CommandText = sql;
            int kq;
            try
            {
                kq = int.Parse(cmd.ExecuteScalar().ToString());
            }
            catch
            {
                conn.Close();
                return 0;
            }
            conn.Close();
            return kq;
        }
        //load thoi gian cho dat ban
        public ThamSoDTO LoadThoiGianChoDatBan()
        {
            ThamSoDTO tsdto = new ThamSoDTO();
            SqlConnection conn = DataProvider.sqlconn;
            conn.Open();
            string sql = "select * from ThamSo";
            SqlDataAdapter dtadp = new SqlDataAdapter(sql, conn);
            try
            {
                DataTable dt = new DataTable();
                dtadp.Fill(dt);
                foreach (DataRow rows in dt.Rows)
                {
                    tsdto.ThoiGianChoDatBan = int.Parse(rows["ThoiGianChoDatBan"].ToString());
                }
            }
            catch
            {
                return null;
            }
            conn.Close();
            return tsdto;
        }
    }
}
